<?php
  require(dirname(__FILE__) . '/includes/init.php');
  //异常签收订单
  $sql = "select o.*,IFNULL(e.express_status,0) as express_status,IFNULL(e.msg_status,0) as msg_status,IFNULL(e.express_time,'') as express_time,IFNULL(e.send_time,0) as send_time,IFNULL(e.query_num,0) as query_num from ".$ecs->table('orders')." as o left join ".$GLOBALS['ecs']->table('express')."as e on o.order_id=e.order_id where express_status=2 order by query_num";
  $error_express_list = $db->getAll($sql);
  foreach($error_express_list as $index=>$express){
	  if($express['express_status']==0){
	      $error_express_list[$index]['express_status'] = '未跟踪';
	  }else if($express['express_status']==1){
		  $error_express_list[$index]['express_status'] = '已签收';
	  }else if($express['express_status']==2){
		  $error_express_list[$index]['express_status'] = '异常件';
	  }else if($express['express_status']==3){
		  $error_express_list[$index]['express_status'] = '派送中';
	  }
	  if($express['msg_status']==0){
	      $error_express_list[$index]['msg_status'] = '未通知';
	  }else if($express['msg_status']==1){
		  $error_express_list[$index]['msg_status'] = '通知成功';
	  }else if($express['msg_status']==2){
		  $error_express_list[$index]['msg_status'] = '通知失败';
	  }
	  if($express['msg_time']!=''){
	       $error_express_list[$index]['msg_time'] = date('Y-m-d H:i:s',$express['msg_time']);
	  }   
	  if($express['express_time']!=''){
	      $error_express_list[$index]['express_time'] = date('Y-m-d H:i:s',$express['express_time']);
	  }
  }
  $templates = $db->getAll("select * from ".$ecs->table('template')); 

  if($_REQUEST['act'] == 'tract'){
	  //除正常签收外的所有订单
	  $sql = "select o.*,IFNULL(e.express_status,0) as express_status,IFNULL(e.msg_status,0) as msg_status,IFNULL(e.express_time,'') as express_time,IFNULL(e.send_time,0) as send_time,IFNULL(e.query_num,0) as query_num from ".$GLOBALS['ecs']->table('orders')." as o left join ".$ecs->table('express')."as e on o.order_id=e.order_id where IFNULL(e.express_status,0)<>1 order by o.order_id";
	  $express_list = $db->getAll($sql);
      //include_once(dirname(__FILE__) ."/includes/modules/kuaidi100/kuaidi100_company.php");
	  foreach($express_list as $index=>$express){
		  //循环
		  $typeCom = $express['express_com'];
		  include_once(dirname(__FILE__) ."/includes/modules/kuaidi100/kuaidi100_company.php");
          //短信模板
		  //$sql = "select content from ".$ecs->table('template')." where template_id='".$_REQUEST['template_id']."'";
		  //$content = $db->getOne($sql);
		  //if($content){
			//$msg = str_replace('{user}',$express['user'],$content);
			//$msg = str_replace('{danhao}',$express['danhao'],$content);
			express_tract($typeCom,$express['danhao'],$express['order_id'],$express['mobile'],'');
		 // }
	 }
	  header("Location: express_list.php\n");
  }

  function express_tract($typeCom,$typeNu,$order_id,$phone,$msg){
	  $get_content = null;
	  if(isset($typeCom)&&isset($typeNu)){
	      $AppKey='9bcd0104e2804506';//请将XXXXXX替换成您在http://kuaidi100.com/app/reg.html申请到的KEY
	      $url ='http://api.kuaidi100.com/api?id='.$AppKey.'&com='.$typeCom.'&nu='.$typeNu.'&show=2&muti=1&order=asc';
	      //请勿删除变量$powered 的信息，否者本站将不再为你提供快递接口服务。
	      $powered = '查询服务由：<a href="http://www.kuaidi100.com" target="_blank" style="color:blue">友商快递100</a> 网站提供';
	      //根据单号和快递公司抓取快递返回信息 $str
		  //优先使用curl模式发送数据
		  if (function_exists('curl_init') == 1){
	          $curl = curl_init();
	          curl_setopt ($curl, CURLOPT_URL, $url);
	          curl_setopt ($curl, CURLOPT_HEADER,0);
	          curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
	          curl_setopt ($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
	          curl_setopt ($curl, CURLOPT_TIMEOUT,5);
	          $get_content = curl_exec($curl);
	          curl_close ($curl);
	      }else{
	          include(dirname(__FILE__) ."/includes/modules/kuaidi100/snoopy.php");
	          $snoopy = new snoopy();
	          $snoopy->referer = 'http://www.google.com/';
	          $snoopy->fetch($url);
	          $get_content = $snoopy->results;
	      }
      }
	  if($get_content){
		  if(strpos($get_content, '已签收')||strpos($get_content, '正常签收')){
	          //发送短信
			  //require(dirname(__FILE__) . '/includes/cls_sms.php');
			  //$str = $sms->send($phone,$msg);
			  $str = null;
			  if($str){
				  if($order_id){
				      $sql = "select express_id from ".$GLOBALS['ecs']->table('express')." where order_id='".$order_id."'";
					  $express_id = $GLOBALS['db']->getOne($sql);
					  if($express_id){
					      $sql = "update ".$GLOBALS['ecs']->table('express')." set express_status=1,msg_status=1,express_time='".gmtime()."',send_time='".gmtime()."',query_num=query_num+1 where express_id='".$express_id."'";
					      $GLOBALS['db']->query($sql);
					  }
				  }else{
					$sql = "insert into ".$GLOBALS['ecs']->table('express')." (order_id,express_status,msg_status,express_time,send_time,query_num) values('".$order_id."',1,1,'".gmtime()."','".gmtime()."',1)";
				    $db->query($sql);
				  }
			  }else{
				  if($order_id){
					  $sql = "select express_id from ".$GLOBALS['ecs']->table('express')." where order_id='".$order_id."'";
					  $express_id = $GLOBALS['db']->getOne($sql);
					  if($express_id){
				          $sql = "update ".$GLOBALS['ecs']->table('express')." set express_status=1,msg_status=2,express_time='".gmtime()."',send_time='".gmtime()."',query_num=query_num+1 where express_id='".$express_id."'";
					      $GLOBALS['db']->query($sql);
				      }else{
						  $sql = "insert into ".$GLOBALS['ecs']->table('express')." (order_id,express_status,msg_status,express_time,send_time,query_num) values('".$order_id."',1,2,'".gmtime()."','".gmtime()."',1)";
				          $GLOBALS['db']->query($sql);
				      }
                  } 
			  }
		  }else if((strpos($get_content, '异常')||strpos($get_content, '漏液'))&&(!strpos($get_content, '已签收')||!strpos($get_content, '正常签收'))){
		       if($order_id){
				      $sql = "select express_id from ".$GLOBALS['ecs']->table('express')." where order_id='".$order_id."'";
					  $express_id = $GLOBALS['db']->getOne($sql);
					  if($express_id){
					      $sql = "update ".$GLOBALS['ecs']->table('express')." set express_status=2,msg_status=0,express_time='".gmtime()."',send_time=0,query_num=query_num+1 where express_id='".$express_id."'";
						  $GLOBALS['db']->query($sql);
					  
				      }else{
					      $sql = "insert into ".$GLOBALS['ecs']->table('express')." (order_id,express_status,msg_status,express_time,send_time,query_num) values('".$order_id."',2,0,'".gmtime()."',0,1)";
				          $GLOBALS['db']->query($sql);
				      }
			   }
	          
		 }else{
	           if($order_id){
				      $sql = "select express_id from ".$GLOBALS['ecs']->table('express')." where order_id='".$order_id."'";
					  $express_id = $GLOBALS['db']->getOne($sql);
					  if($express_id){
					      $sql = "update ".$GLOBALS['ecs']->table('express')." set express_status=3,msg_status=0,express_time='".gmtime()."',send_time=0,query_num=query_num+1 where express_id='".$express_id."'";
						  $GLOBALS['db']->query($sql);
					  }else{
					      $sql = "insert into ".$GLOBALS['ecs']->table('express')." (order_id,express_status,msg_status,express_time,send_time,query_num) values('".$order_id."',3,0,'".gmtime()."',0,1)";
				          $GLOBALS['db']->query($sql);
				      }  
		      }
		 }
	 }
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 快递列表 </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/general.css" rel="stylesheet" type="text/css" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
<h1>
<span class="action-span"><a href="message.php">添加短信模板</a></span>
<span class="action-span1"><a target="_top" href="index.php?act=main">管理中心</a> </span><span id="search_id" class="action-span1"> - 快递列表 </span>
<div style="clear:both"></div>
</h1>
<div class="form-div">
   <form action="express_list.php" method="post" name="searchForm">
    <!-- 短信模板
   <select name="template_id" id="template_id">
    <option value="0">请选择</option>
	  <?php foreach($templates as $template){?>
	  <option value="<?php echo $template['template_id'];?>"><?php echo $template['title'];?></option>
	  <?php }?>
   </select>-->
   <input type="hidden" name="act" value="tract" />
   <input type="submit" value=" 快递查询 " class="button" onclick="search()"/>   
   &nbsp;&nbsp;<span id="jindu" style="display:none;"><img src="images/jindu.gif" />&nbsp;&nbsp;查询时，请勿做其他操作</span>
   </form>
</div>
<script type="text/javascript">
function select_templ(){
	if($("#template_id").val()==0){
		alert("请选择短信模板");
		return false;
	}else{
		return true;
	}
}
function search(){
	$("#jindu").css("display","block");
}
</script>
<!-- 订单列表 -->
  <div class="list-div" id="listDiv">
 
<table cellpadding="3" cellspacing="1">
  <tr>
    <th>系统订单号</th>
    <th>收货人</th>
    <th>产品</th>
    <th>手机</th>
	<th>地址</th>
    <th>快递公司</th>
	<th>快递单号</th>
    <th>快递状态</th>
	<th>短信状态</th>
	<th>通知时间</th>
	<th>跟踪时间</th>
    <th>查询次数</th>
  <tr>
  <?php 
  if(count($error_express_list)){
	  $i=0;
	foreach($error_express_list AS $index => $service){
		$i++;
  ?>
   <tr>
    <td><?php echo $service['sys_order_sn']?></td>
    <td><?php echo $service['user']?></td>
    <td><?php echo $service['product_name']?></td>
    <td><?php echo $service['mobile']?></td>
	<td><span id="order_<?php echo $i;?>"><?php echo sub_str($service['address'],10);?></span><div id="address_<?php echo $i;?>" style="height:20px;margin-top:-1px;position:absolute;color:#fff;background-color:red;display:none"><?php echo $service['address'];?></div></td>
	<td><?php echo $service['express_com'];?></td>
	<td><a href="express.php?com=<?php echo $service['express_com']?>&nu=<?php echo $service['danhao'];?>" target="_blank"><?php echo $service['danhao'];?></a></td>
	<td><?php echo $service['express_status'];?></td>
	<td><?php echo $service['msg_status'];?></td>
	<td><?php echo $service['msg_time'];?></td>
	<td><?php echo $service['express_time'];?></td>
    <td><?php echo $service['query_num'];?></td> 
  <tr>
  <?php } } ?>
  </table>
 
  </div>
<script type="text/javascript">
var num = <?php echo count($services);?>;
for (i=1;i<=num;i++){
    hide_show_1(i);
	hide_show_2(i);
}
function hide_show_1(i){
	$("#td_"+i).hover(function() {
        $("#contents_"+i).show();  
    },function(){
           $("#contents_"+i).fadeOut(1000);  
        }

   ); 
}
function hide_show_2(i){

	$("#order_"+i).hover(function() {         
        $("#address_"+i).show();  
    },function(){
           $("#address_"+i).fadeOut(1000);  
        }

   ); 
}
function from(){
	var from_where = $("#from_where").val();
	var url = "<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?>";
	if(url.indexOf("?") > 0 )
	{
		window.location.href = url +'&from_where='+from_where;
	}else{
		window.location.href = url+'?from_where='+from_where;  
	}
}
</script>
</body>
</html>